home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / player_repeater.js < prev    next >
Text File  |  2006-02-07  |  17KB  |  452 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. try
  28. {
  29.  
  30. var thePlayerRepeater = null;
  31.  
  32. function CNullStub()
  33. {
  34. //  alert( "ERROR: someone called the nullstub." );
  35. }
  36.  
  37. function onPlaybackEventStub( key, value )
  38. {
  39.   // Doesn't do anything, just a stub.
  40. }
  41.  
  42. //
  43. // Player Repeater
  44. //
  45.  
  46. // The player repeater object is a single global thunk layer between the player remotes and the embedded playback cores.
  47. function CPlayerRepeater()
  48. {
  49.   // Hook it up
  50.   var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  51.   jsLoader.loadSubScript( "chrome://rmp_demo/content/songbird_interfaces.js", null );
  52.   jsLoader.loadSubScript( "chrome://rmp_demo/content/sbIDataRemote.js", null );
  53.  
  54.   // First we need our internal API, originally stubbed to NULL
  55.   this.doPlayImmediate_i  = CNullStub;
  56.   this.doPlay_i           = CNullStub;
  57.   this.doPause_i          = CNullStub;
  58.   this.doStop_i           = CNullStub;
  59.   this.doNext_i           = CNullStub;
  60.   this.doPrev_i           = CNullStub;
  61.   this.isPlaying_i        = CNullStub;
  62.   this.isPaused_i         = CNullStub;
  63.   this.getLength_i        = CNullStub;
  64.   this.getPosition_i      = CNullStub;
  65.   this.setPosition_i      = CNullStub;
  66.   this.getVolume_i        = CNullStub;
  67.   this.setVolume_i        = CNullStub;
  68.   this.getMute_i          = CNullStub;
  69.   this.setMute_i          = CNullStub;
  70.   this.getMetadata_i      = CNullStub;
  71.   this.getURLMetadata_i   = CNullStub;
  72.   
  73.   // Then we declare our external API, which encodes the function call through the dataremote.
  74.   this.doPlayImmediate = function doPlayImmediate( url )
  75.   {
  76.     SBDataSetValue( "thePlayerRepeater.doPlayImmediate.url", url );
  77.     SBDataFireEvent( "thePlayerRepeater.doPlayImmediate" );
  78.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  79.   }
  80.   this.doPlay          = function doPlay()
  81.   {
  82.     SBDataFireEvent( "thePlayerRepeater.doPlay" );
  83.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  84.   }
  85.   this.doPause         = function doPause()
  86.   {
  87.     SBDataFireEvent( "thePlayerRepeater.doPause" );
  88.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  89.   }
  90.   this.doStop          = function doStop()
  91.   {
  92.     SBDataFireEvent( "thePlayerRepeater.doStop" );
  93.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  94.   }
  95.   this.doNext          = function doNext()
  96.   {
  97.     SBDataFireEvent( "thePlayerRepeater.doNext" );
  98.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  99.   }
  100.   this.doPrev          = function doPrev()
  101.   {
  102.     SBDataFireEvent( "thePlayerRepeater.doPrev" );
  103.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  104.   }
  105.   this.isPlaying       = function isPlaying()
  106.   {
  107.     SBDataFireEvent( "thePlayerRepeater.isPlaying" );
  108.     return SBDataGetIntValue( "thePlayerRepeater.retval" ) != 0;
  109.   }
  110.   this.isPaused        = function isPaused()
  111.   {
  112.     SBDataFireEvent( "thePlayerRepeater.isPaused" );
  113.     return SBDataGetIntValue( "thePlayerRepeater.retval" ) != 0;
  114.   }
  115.   this.getLength     = function getLength()
  116.   {
  117.     SBDataFireEvent( "thePlayerRepeater.getLength" );
  118.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  119.   }
  120.   this.getPosition     = function getPosition()
  121.   {
  122.     SBDataFireEvent( "thePlayerRepeater.getPosition" );
  123.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  124.   }
  125.   this.setPosition     = function setPosition( pos )
  126.   {
  127.     SBDataSetValue( "thePlayerRepeater.setPosition.pos", pos );
  128.     SBDataFireEvent( "thePlayerRepeater.setPosition" );
  129.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  130.   }
  131.   this.getVolume       = function getVolume()
  132.   {
  133.     SBDataFireEvent( "thePlayerRepeater.getVolume" );
  134.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  135.   }
  136.   this.setVolume       = function setVolume( vol )
  137.   {
  138.     SBDataSetValue( "thePlayerRepeater.setVolume.vol", vol );
  139.     SBDataFireEvent( "thePlayerRepeater.setVolume" );
  140.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  141.   }
  142.   this.getMute         = function getMute()
  143.   {
  144.     SBDataFireEvent( "thePlayerRepeater.getMute" );
  145.     return SBDataGetIntValue( "thePlayerRepeater.retval" ) != 0;
  146.   }
  147.   this.setMute         = function setMute( mute )
  148.   {
  149.     SBDataSetValue( "thePlayerRepeater.setMute.mute", mute );
  150.     SBDataFireEvent( "thePlayerRepeater.setMute" );
  151.     return SBDataGetIntValue( "thePlayerRepeater.retval" );
  152.   }
  153.   this.getMetadata     = function getMetadata( key )
  154.   {
  155.     SBDataSetValue( "thePlayerRepeater.getMetadata.key", key );
  156.     SBDataFireEvent( "thePlayerRepeater.getMetadata" );
  157.     return SBDataGetValue( "thePlayerRepeater.retval" );
  158.   }
  159.   this.getURLMetadata  = function getURLMetadata( url, keys )
  160.   {
  161.     SBDataSetValue( "thePlayerRepeater.getURLMetadata.url", url );
  162.     SBDataSetValue( "thePlayerRepeater.getURLMetadata.keys.length", keys.length );
  163.     for ( var i = 0; i < keys.length; i++ )
  164.     {
  165.       SBDataSetValue( "thePlayerRepeater.getURLMetadata.keys." + i, keys[ i ] );
  166.     }
  167.     SBDataFireEvent( "thePlayerRepeater.getURLMetadata" );
  168.     var num = SBDataGetIntValue( "thePlayerRepeater.retval.length" );
  169.     var retval = new Array();
  170.     for ( var i = 0; i < num; i++ )
  171.     {
  172.       retval.push( SBDataGetValue( "thePlayerRepeater.retval." + i ) );
  173.     }
  174.     return retval;
  175.   }
  176.   
  177.   
  178.   // Then specify the middle callback API that pumps the calls to the inner API
  179.   this.cb_doPlayImmediate = function()
  180.   {
  181.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  182.     var url = SBDataGetValue( "thePlayerRepeater.doPlayImmediate.url" );
  183.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doPlayImmediate_i( url ) );
  184.   }
  185.   this.cb_doPlay          = function()
  186.   {
  187.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  188.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doPlay_i() );
  189.   }
  190.   this.cb_doPause         = function()
  191.   {
  192.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  193.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doPause_i() );
  194.   }
  195.   this.cb_doStop          = function()
  196.   {
  197.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  198.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doStop_i() );
  199.   }
  200.   this.cb_doNext          = function()
  201.   {
  202.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  203.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doNext_i() );
  204.   }
  205.   this.cb_doPrev          = function()
  206.   {
  207.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  208.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.doPrev_i() );
  209.   }
  210.   this.cb_isPlaying       = function()
  211.   {
  212.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  213.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.isPlaying_i() );
  214.   }
  215.   this.cb_isPaused        = function()
  216.   {
  217.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  218.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.isPaused_i() );
  219.   }
  220.   this.cb_getLength       = function()
  221.   {
  222.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  223.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.getLength_i() );
  224.   }
  225.   this.cb_getPosition     = function()
  226.   {
  227.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  228.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.getPosition_i() );
  229.   }
  230.   this.cb_setPosition     = function()
  231.   {
  232.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  233.     var pos = SBDataGetValue( "thePlayerRepeater.setPosition.pos" );
  234.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.setPosition_i( pos ) );
  235.   }
  236.   this.cb_getVolume       = function()
  237.   {
  238.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  239.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.getVolume_i() );
  240.   }
  241.   this.cb_setVolume       = function()
  242.   {
  243.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  244.     var vol = SBDataGetValue( "thePlayerRepeater.setVolume.vol" );
  245.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.setVolume_i( vol ) );
  246.   }
  247.   this.cb_getMute         = function()
  248.   {
  249.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  250.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.getMute_i() );
  251.   }
  252.   this.cb_setMute         = function()
  253.   {
  254.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  255.     var mute = SBDataGetValue( "thePlayerRepeater.setMute.mute" );
  256.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.setMute_i( mute ) );
  257.   }
  258.   this.cb_getMetadata     = function()
  259.   {
  260.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  261.     var key = SBDataGetValue( "thePlayerRepeater.getMetadata.key" );
  262.     return SBDataSetValue( "thePlayerRepeater.retval", thePlayerRepeater.getMetadata_i( key ) );
  263.   }
  264.   this.cb_getURLMetadata  = function( url, keys )
  265.   {
  266.     if ( !thePlayerRepeater || thePlayerRepeater.binding ) return;
  267.     var url = SBDataGetValue( "thePlayerRepeater.getURLMetadata.url" );
  268.     var num = SBDataGetValue( "thePlayerRepeater.getURLMetadata.keys.length" );
  269.     var keys = new Array();
  270.     for ( var i = 0; i < num; i++ )
  271.     {
  272.       keys.push( SBDataGetValue( "thePlayerRepeater.getURLMetadata.keys." + i ) );
  273.     }
  274.     var values = thePlayerRepeater.getURLMetadata_i( url, keys );
  275.     SBDataSetValue( "thePlayerRepeater.retval.length", values.length );
  276.     for ( var i = 0; i < values.length; i++ )
  277.     {
  278.       SBDataSetValue( "thePlayerRepeater.retval." + i, values[ i ] );
  279.     }
  280.   }
  281.   
  282.   // Then bind the callback API into a set of dataremotes
  283.   this.remotes = new Array();
  284.   this.bind = function( func, key )
  285.   {
  286.     var remote = new sbIDataRemote( key );
  287.     remote.BindCallbackFunction( func );
  288.     this.remotes.push( remote );
  289.   }
  290.   this.unbind = function()
  291.   {
  292.     for ( var i in this.remotes )
  293.     {
  294.       this.remotes[ i ].Unbind();
  295.     }
  296.   }
  297.  
  298.   // setPlaybackCore maps the function refs from the passed core into the internal API refs
  299.   this.setPlaybackCore = function setPlaybackCore( core )
  300.   {
  301.     // Inform the current core that it is going away.
  302.     if ( typeof( thePlayerRepeater.m_CurrentCore ) != 'undefined' )
  303.     {
  304.       this.m_CurrentCore.onSwappedOut();
  305.     }
  306.     // Copy this core in.
  307.     this.m_CurrentCore     = core;
  308.     this.doPlayImmediate_i = core.doPlayImmediate;
  309.     this.doPlay_i          = core.doPlay;
  310.     this.doPause_i         = core.doPause;
  311.     this.doStop_i          = core.doStop;
  312.     this.doNext_i          = core.doNext;
  313.     this.doPrev_i          = core.doPrev;
  314.     this.isPlaying_i       = core.isPlaying;
  315.     this.isPaused_i        = core.isPaused;
  316.     this.getLength_i       = core.getLength;
  317.     this.getPosition_i     = core.getPosition;
  318.     this.setPosition_i     = core.setPosition;
  319.     this.getVolume_i       = core.getVolume;
  320.     this.setVolume_i       = core.setVolume;
  321.     this.getMute_i         = core.getMute;
  322.     this.setMute_i         = core.setMute;
  323.     this.getMetadata_i     = core.getMetadata;
  324.     this.getURLMetadata_i  = core.getURLMetadata;
  325.     
  326.     this.binding = true;
  327.     if ( this.remotes.length == 0 )
  328.     {
  329.       this.bind( this.cb_doPlayImmediate, "thePlayerRepeater.doPlayImmediate" );
  330.       this.bind( this.cb_doPlay, "thePlayerRepeater.doPlay" );
  331.       this.bind( this.cb_doPause, "thePlayerRepeater.doPause" ); 
  332.       this.bind( this.cb_doStop, "thePlayerRepeater.doStop" );
  333.       this.bind( this.cb_doNext, "thePlayerRepeater.doNext" );
  334.       this.bind( this.cb_doPrev, "thePlayerRepeater.doPrev" );
  335.       this.bind( this.cb_isPlaying, "thePlayerRepeater.isPlaying" );
  336.       this.bind( this.cb_isPaused, "thePlayerRepeater.isPaused" );
  337.       this.bind( this.cb_getLength, "thePlayerRepeater.getLength" );
  338.       this.bind( this.cb_getPosition, "thePlayerRepeater.getPosition" );
  339.       this.bind( this.cb_setPosition, "thePlayerRepeater.setPosition" );
  340.       this.bind( this.cb_getVolume, "thePlayerRepeater.getVolume" );
  341.       this.bind( this.cb_setVolume, "thePlayerRepeater.setVolume" );
  342.       this.bind( this.cb_getMute, "thePlayerRepeater.getMute" );
  343.       this.bind( this.cb_setMute, "thePlayerRepeater.setMute" );
  344.       this.bind( this.cb_getMetadata, "thePlayerRepeater.getMetadata" );
  345.       this.bind( this.cb_getURLMetadata, "thePlayerRepeater.getURLMetadata" );
  346.     }
  347.     this.binding = false;
  348.   }
  349.   
  350.   // We need an array of player instances, to route the onPlaybackEvent call.
  351.   this.m_RemotesArray = new Array();
  352.   
  353.   // Here's the functions for putting remotes into and out of the array.
  354.   this.registerRemote = function registerRemote( remote )
  355.   {
  356.     this.m_RemotesArray[ this.m_RemotesArray.length ] = remote;
  357.   }
  358.   
  359.   this.deregisterRemote = function deregisterRemote( remote )
  360.   {
  361.     for ( var i in this.m_RemotesArray )
  362.     {
  363.       // Find it
  364.       if ( this.m_RemotesArray[ i ] == remote )
  365.       {
  366.         // Inform it that it got killed
  367.         this.m_RemotesArray[ i ].onPlaybackEvent( "shutdown", 1 );
  368.         
  369.         // And cut it
  370.         this.m_RemotesArray.splice( i, 1 );
  371.       }
  372.     }
  373.   }
  374.   
  375.   // And, lastly the callback repeater
  376.   this.onPlaybackEvent = function onPlaybackEvent( key, value )
  377.   {
  378.     for ( var i = 0; i < this.m_RemotesArray.length; i++ )
  379.     {
  380.       this.m_RemotesArray[ i ].onPlaybackEvent( key, value );
  381.     }
  382.   }  
  383. }
  384. // Woo, then we instantiate the global instance of the repeater 
  385. // (Don't touch the global instance, tho, just instantiate your own CPlayerRemote)
  386. thePlayerRepeater = new CPlayerRepeater();
  387.  
  388. //
  389. // Player Remote
  390. //
  391.  
  392. function CPlayerRemote( onPlaybackEventHandler )
  393. {
  394.   // The user code should specify an event handler.  If not, it gets the stub.  
  395.   // Yay for the magic "OR" operator that does something completely different than you'd expect
  396.   // (if the first operand is NULL, assign the second)
  397.   this.onPlaybackEvent = onPlaybackEventHandler || onPlaybackEventStub;
  398.   
  399.   // All the methods on the remote simply call the methods on the singleton.
  400.   this.doPlayImmediate = thePlayerRepeater.doPlayImmediate;
  401.   this.doPlay          = thePlayerRepeater.doPlay;
  402.   this.doPause         = thePlayerRepeater.doPause;
  403.   this.doStop          = thePlayerRepeater.doStop;
  404.   this.doNext          = thePlayerRepeater.doNext;
  405.   this.doPrev          = thePlayerRepeater.doPrev;
  406.   this.isPlaying       = thePlayerRepeater.isPlaying;
  407.   this.isPaused        = thePlayerRepeater.isPaused;
  408.   this.getLength       = thePlayerRepeater.getLength;
  409.   this.getPosition     = thePlayerRepeater.getPosition;
  410.   this.setPosition     = thePlayerRepeater.setPosition;
  411.   this.getVolume       = thePlayerRepeater.getVolume;
  412.   this.setVolume       = thePlayerRepeater.setVolume;
  413.   this.getMute         = thePlayerRepeater.getMute;
  414.   this.setMute         = thePlayerRepeater.setMute;
  415.   this.getMetadata     = thePlayerRepeater.getMetadata;
  416.   this.getURLMetadata  = thePlayerRepeater.getURLMetadata;
  417.   
  418.   // Make the explicit destruct method (hooray for pissy javascript!)
  419.   this.destruct = function destruct()
  420.   {
  421.     // When we are (explicitly!) destructificated, remove ourselves
  422.     thePlayerRepeater.deregisterRemote( this );
  423.   }
  424.   
  425.   // Lastly register our remote with the repeater
  426.   thePlayerRepeater.registerRemote( this );
  427.   
  428. }
  429.  
  430. var UNDEFINED = 0;
  431. var STOPPED = 1 
  432. var PAUSED = 2;
  433. var PLAYING = 3 
  434. var SCANFORWARD = 4;
  435. var SCANREVERSE = 5 
  436. var BUFFERING = 6;
  437. var WAITING = 7;
  438. var ENDED = 8 
  439. var TRANSITIONING = 9 
  440. var READY = 10
  441. var RECONNECTING = 11;
  442.  
  443. }
  444. catch ( err )
  445. {
  446.   var text = "\r\n";
  447.   for ( var i in err )
  448.   {
  449.     text += err[ i ] + "\r\n";
  450.   }
  451.   alert( "player_repeater.js - init - " + err + text );
  452. }